package screen
{
	import com.adobe.serialization.json.JSON;
	import com.greensock.TweenLite;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	
	import helper.Services;
	import helper.ValueObject;
	
	import screen.component.ExitGame;
	import screen.component.Help;
	import screen.component.Main;
	import screen.component.Option;
	import screen.component.Popup;
	import screen.component.Rank;
	import screen.component.Rule;
	import screen.component.UI;
	
	public class TechComBankScreen extends UI
	{
		private const _MILLION:String =" Triệu"
		private var _bg:MovieClip;
		private var _exit_game_cl:ExitGame;
		private var _help_cl:Help;
		private var _main_cl:Main;
		private var _option_cl:Option;
		private var _rank_cl:Rank;
		private var _rule_cl:Rule;
		private var _curren_show:Popup;
		public static var instance:TechComBankScreen;
		public function TechComBankScreen()
		{
			if(!instance){
				instance = this;
			}
		}
		public static function getInstance():TechComBankScreen{
			if(!instance){
				instance = new TechComBankScreen();
			}
			return instance;
		}
		public function onShow():void{
			init();
		}
		public function onHide():void{
			
		}
		private function init():void{
			_bg = ValueObject.T1_RESOURCE.getExportedAsset("bg") as MovieClip;
			with(_bg){
				buttonImageInit(playBtn);
				MovieClip(playBtn).addEventListener(MouseEvent.CLICK,playBtnHandle);
				buttonImageInit(closeBtn);
				MovieClip(closeBtn).addEventListener(MouseEvent.CLICK,closeBtnHandle);
				buttonImageInit(rankBtn);
				MovieClip(rankBtn).addEventListener(MouseEvent.CLICK,rankBtnHandle);
				buttonImageInit(helpBtn);
				MovieClip(helpBtn).addEventListener(MouseEvent.CLICK,helpBtnHandle);
				buttonImageInit(toTalBonusBtn);
				MovieClip(toTalBonusBtn).addEventListener(MouseEvent.CLICK,totalBonusHandle);
				buttonImageInit(ruleBtn);
				MovieClip(ruleBtn).addEventListener(MouseEvent.CLICK,ruleBtnHandle);
			}
			 
			addChild(_bg);
			MovieClip(_bg.bonus).gotoAndStop(1);
			
			_exit_game_cl = new ExitGame();
			addChild(_exit_game_cl);
			
			_help_cl = new Help();
			addChild(_help_cl);
			
			_main_cl = new Main();
			addChild(_main_cl);
			
			_option_cl = new Option();
			addChild(_option_cl);
			
			_rank_cl = new Rank();
			addChild(_rank_cl);
			
			_rule_cl = new Rule;
			addChild(_rule_cl);
			
			_curren_show = _option_cl;
			Services.getInstance().getResource(getResourceComplete,getResourceError);
			
			resize();
			this.stage.addEventListener(Event.RESIZE, resize);
		}
		private function resize():void{
			ValueObject.WIDTH = this.stage.stageWidth;
			ValueObject.HEIGHT = this.stage.stageHeight;
			_bg.x = (ValueObject.WIDTH - _bg.bgTech.width)/2;
			_bg.y = (ValueObject.HEIGHT - _bg.bgTech.height)/2;
		}
		public function playBtnHandle(e:MouseEvent):void{
			showPopup(_main_cl);
		}
		private function closeBtnHandle(e:MouseEvent):void{
			showPopup(_exit_game_cl);
		}
		private function rankBtnHandle(e:MouseEvent):void{
			showPopup(_rank_cl);
		}
		public function helpBtnHandle(e:MouseEvent):void{
			showPopup(_help_cl);
		}
		private function totalBonusHandle(e:MouseEvent):void{
			navigateToURL(new URLRequest("http://quetthegiangsinh.com/game-giang-sinh/giai-thuong-game/"),"_parent");
		}
		private function ruleBtnHandle(e:MouseEvent):void{
			navigateToURL(new URLRequest("http://quetthegiangsinh.com/game-giang-sinh/the-le-game/"),"_parent");
		}
		private function showPopup(popup:Popup):void{
			if(_curren_show == popup){
				return;
			}
			popup.show();
			_curren_show.hide();
			_curren_show = popup;
		}
		private function getResourceComplete(e:Event):void{
			//return;
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getResourceComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getResourceError);
			var ob:Object = JSON.decode(e.target.data);
			var tem:Number = Math.max(5,30-Math.floor(ob.score/50));
			_main_cl.step = 100/tem;
			TextField(_bg.scoreTf).text = String(ob.score);
			var level : int = ob.score / 100 +1;
			TextField(_bg.levelTf).text = String(level);
		}
		private function getResourceError(e:IOErrorEvent):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getResourceComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getResourceError);
		}
		public function addScore(score:Number):void{
			if((parseInt(_bg.scoreTf.text) + score)%50 == 0){
				score +=10;
			}
			MovieClip(_bg.bonus).gotoAndStop(1);
			TextField(_bg.bonus.mcBonus.txtNumber).text = '+'+score +" điểm";
			MovieClip(_bg.bonus).play();
			TextField(_bg.scoreTf).text = String(parseInt(_bg.scoreTf.text) + score);
			var tem:Number = Math.max(5,30-Math.floor(parseInt(_bg.scoreTf.text)/50));
			_main_cl.step = 100/tem;
		}
		public function get bg():MovieClip{
			return _bg;
		}	
		private function formatNumber(no:Number):String{
			if(no<10){
				return "0"+no.toString();
			}else{
				return no.toString();
			}
		}
	}
}